package com.htj.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htj.admin.dao.FoodTypeDao;
import com.htj.admin.dao.OrderDao;
import com.htj.admin.dao.OrderDetailDao;
import com.htj.admin.domain.Food;
import com.htj.admin.domain.FoodType;
import com.htj.admin.domain.Order;
import com.htj.admin.domain.OrderDetail;
import com.htj.admin.domain.dto.LineDto;
import com.htj.admin.service.CartServiceInf;
import com.htj.admin.service.FoodServiceInf;
import com.htj.admin.service.FoodTypeServiceInf;
import com.htj.admin.service.OrderServiceInf;
import com.htj.util.DateTimeUtil;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderServiceInf {
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderDetailDao orderDetailDao;
    @Autowired
    CartServiceInf cartServiceInf;
    @Autowired
    private FoodServiceInf foodServiceInf;

    @Override
    public boolean add(Order order) {
        boolean bool = false;
        //设置时间
        String time = DateTimeUtil.getSysTime();
        order.setCreateTime(time);
//        设置订单编号
        String orderNo = DateUtil.format(new Date(),"yyyyMMdd")+System.currentTimeMillis();
        order.setOrderNo(orderNo);
//        设置支付状态为0未支付
        order.setPay("0");
//          设置订单状态0 未生效
        order.setStatus("0");

        //        添加订单详情
        List<Food> foods = order.getFoods();//这个food只有id和sum 2个属性
        for(Food food:foods){
            //添加订单详情
            bool = orderDetailDao.add(food,orderNo);
            //        删除购物车
            cartServiceInf.deleteByfoodIdAnduserId(Integer.valueOf(order.getUserId()),food.getId());
            //减少库存增加销量
            bool = foodServiceInf.changeSaleAndSum(food);
        }

        int flag = orderDao.add(order);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public PageListVo<Order> pageList(Map<String, String> map) {
        PageListVo<Order> pageListVo = new PageListVo<>();
        String currentPage = map.get("currentPage");
        String pageSize = map.get("pageSize");
        PageHelper.startPage(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
        List<Order> list = orderDao.pageList(map);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        List<Order> pageInfoList = pageInfo.getList();
        int total = (int) pageInfo.getTotal();
        pageListVo.setList(pageInfoList);
        pageListVo.setTotal(total);
        return pageListVo;
    }


    @Override
    public boolean delete(Integer id) {
        boolean bool = false;
        int flag = orderDao.delete(id);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public boolean changeStatus(Map<String,String> map) {
        boolean bool = false;
        int flag = orderDao.changeStatus(map);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public List<OrderDetail> detail(String id) {
        List<OrderDetail> list = orderDetailDao.detail(id);
        return list;
    }

    @Override
    public PageListVo<Order> searchStatus(HashMap<String, String> map) {
        PageListVo<Order> pageListVo = new PageListVo<>();
        String currentPage = map.get("currentPage");
        String pageSize = map.get("pageSize");
        PageHelper.startPage(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
        List<Order> list = orderDao.searchStatus(map);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        List<Order> pageInfoList = pageInfo.getList();
        int total = (int) pageInfo.getTotal();
        pageListVo.setList(pageInfoList);
        pageListVo.setTotal(total);
        return pageListVo;
    }

    @Override
    public List<Order> getAll() {
        List<Order> list = orderDao.getAll();
        return list;
    }

    @Override
    public Order getById(String id) {
        Order order = orderDao.getById(id);
        return order;
    }

    @Override
    public boolean changePay(Map<String, String> map) {
        boolean bool = false;
        int flag = orderDao.changePay(map);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }


}
